package nl.windgazer.arachne.internal.transform;

import java.lang.reflect.Modifier;

import javax.servlet.http.HttpServletRequest;

import nl.windgazer.arachne.annotations.IncludeFaceBookOpenGraphInfo;
import nl.windgazer.arachne.internal.URLHelper;
import nl.windgazer.arachne.services.ArachneRenderSupport;
import nl.windgazer.arachne.services.facebook.FacebookShareMediumType;

import org.apache.log4j.Logger;
import org.apache.tapestry5.Asset;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.func.Worker;
import org.apache.tapestry5.ioc.Resource;
import org.apache.tapestry5.ioc.services.SymbolSource;
import org.apache.tapestry5.model.MutableComponentModel;
import org.apache.tapestry5.plastic.PlasticClass;
import org.apache.tapestry5.runtime.Component;
import org.apache.tapestry5.services.AssetSource;
import org.apache.tapestry5.services.ClassTransformation;
import org.apache.tapestry5.services.ComponentInstanceOperation;
import org.apache.tapestry5.services.ComponentMethodAdvice;
import org.apache.tapestry5.services.ComponentMethodInvocation;
import org.apache.tapestry5.services.FieldAccess;
import org.apache.tapestry5.services.TransformConstants;
import org.apache.tapestry5.services.TransformField;
import org.apache.tapestry5.services.TransformMethod;
import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
import org.apache.tapestry5.services.transform.TransformationSupport;

/**
 * 
 * @author mreuring
 *
 */
public class IncludeFaceBookOpenGraphInfoWorker implements ComponentClassTransformWorker2 {

//	private final static Logger logger = Logger.getLogger(IncludeFaceBookOpenGraphInfoWorker.class);
//
//	private final ArachneRenderSupport aRenderSupport;
//	private final SymbolSource symbolSource;
//	private final HttpServletRequest request;
//
//	private class AnotationWorker extends Worker<IncludeFaceBookOpenGraphInfo> {
//
//		private ComponentResources resources = null;
//
//        public void work(IncludeFaceBookOpenGraphInfo annotation)
//        {
//        	if (resources != null) {
//		        final String imgUrl = annotation.image();
//		        final boolean imgIsAbs = imgUrl.startsWith("http");
//	
//		        setTitle(symbolSource.expandSymbols(annotation.title()), aRenderSupport, request, resources);
//				IncludeOpenGraphMetaInfoWorker.addOpenGraphMetaInfo("type", annotation.type().toString(), aRenderSupport, resources);
//				if (annotation.url().length() > 0) {
//					setUrl(annotation.url(), aRenderSupport, request, resources);
//				}
//				if (imgUrl.length() > 0 && imgIsAbs) {
//					setImage(imgUrl, aRenderSupport, request);
//				}
//		        IncludeOpenGraphMetaInfoWorker.addOpenGraphMetaInfo("site_name", annotation.site_name(), aRenderSupport, resources);
//				if (annotation.admins().length() > 0) IncludeOpenGraphMetaInfoWorker.addOpenGraphMetaInfo("admins", annotation.admins(), aRenderSupport, resources);
//				if (annotation.app_id().length() > 0) IncludeOpenGraphMetaInfoWorker.addOpenGraphMetaInfo("app_id", annotation.app_id(), aRenderSupport, resources);
//				if (annotation.description().length() > 0) {
//					setDescription(symbolSource.expandSymbols(annotation.description()), aRenderSupport, request, resources);
//				}
//				if (!FacebookShareMediumType.none.equals(annotation.medium())) {
//					aRenderSupport.addMetaTag("medium", annotation.medium().toString());
//				}
//	
//		        if (imgUrl.length() > 0 && !imgIsAbs) {
//		        	logger.error("NYI!!! Working with an asset for adding img url is not yet implemented!!!");
////		        	addOperationForAssetPaths(transformation, model, new String[]{annotation.image()});
//		        }
//        	}
//        }
//        
//        public void setResources(ComponentResources resources) {
//        	this.resources = resources;
//        }
//        
//	}
//	
//	private final AnotationWorker setOpenGraphWorker = new AnotationWorker();
//
//	public IncludeFaceBookOpenGraphInfoWorker(
//			ArachneRenderSupport aRenderSupport
//			, AssetSource assetSource
//			, SymbolSource symbolSource
//			, HttpServletRequest request
//			)
//    {
//        this.aRenderSupport = aRenderSupport;
//        this.symbolSource = symbolSource;
//        this.request = request;
//    }
//
//	public void transform(ClassTransformation transformation, MutableComponentModel model) {
//		final IncludeFaceBookOpenGraphInfo annotation = transformation.getAnnotation(IncludeFaceBookOpenGraphInfo.class);
//		if (annotation != null) {
//	        TransformMethod setupRender = transformation.getOrCreateMethod(TransformConstants.SETUP_RENDER_SIGNATURE);
//
//	        setOpenGraphByAnnotation(transformation, model, setupRender, annotation);
//
//	        model.addRenderPhase(SetupRender.class);
//		}
//	}
//
//    private void setOpenGraphByAnnotation(ClassTransformation transformation, MutableComponentModel model,
//            TransformMethod method, IncludeFaceBookOpenGraphInfo annotation) {
//    	decorateMethodWithDifferentOperation(transformation, model, method, annotation, setOpenGraphWorker);
//    }
//
//    protected void decorateMethodWithDifferentOperation(ClassTransformation transformation, MutableComponentModel model,
//            TransformMethod method, IncludeFaceBookOpenGraphInfo annotation, Worker<IncludeFaceBookOpenGraphInfo> operation) {
//		final FieldAccess cfFieldAccess = createFieldForIncludeFaceBookOpenGraphInfo(transformation);
//		storeLocalizedAssetsAtPageLoad(transformation, model.getBaseResource(), annotation, cfFieldAccess);
//		addMethodAssetOperationAdvice(method, cfFieldAccess, operation);
//    }
//
//    protected FieldAccess createFieldForIncludeFaceBookOpenGraphInfo(ClassTransformation transformation)
//    {
//        TransformField field = transformation.createField(Modifier.PROTECTED, IncludeFaceBookOpenGraphInfo.class.getName(), "includedIncludeFaceBookOpenGraphInfo");
//
//        return field.getAccess();
//    }
//
//    protected void storeLocalizedAssetsAtPageLoad(ClassTransformation transformation, final Resource baseResource,
//    		final IncludeFaceBookOpenGraphInfo annotation, final FieldAccess access)
//    {
//        ComponentMethodAdvice advice = new ComponentMethodAdvice()
//        {
//            public void advise(ComponentMethodInvocation invocation)
//            {
//                invocation.proceed();
//
//                access.write(invocation.getInstance(), annotation);
//            }
//        };
//
//        transformation.getOrCreateMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE).addAdvice(advice);
//    }
//
//    protected void addMethodAssetOperationAdvice(TransformMethod method, final FieldAccess access,
//            final Worker<IncludeFaceBookOpenGraphInfo> operation)
//    {
//        ComponentInstanceOperation advice = new ComponentInstanceOperation()
//        {
//
//			public void invoke(Component instance)
//            {
//                IncludeFaceBookOpenGraphInfo annotation = (IncludeFaceBookOpenGraphInfo) access.read(instance);
//
//                ((AnotationWorker)operation).setResources(instance.getComponentResources());
//                operation.work(annotation);
//                ((AnotationWorker)operation).setResources(null);
//
//            }
//        };
//
//        method.addOperationBefore(advice);
//    }
//
//	protected void handleAsset(Asset img) {
//		setImage(img.toClientURL(), aRenderSupport, request);
//	}
//
//	public static void setImage(final String img, final ArachneRenderSupport rs, HttpServletRequest request) {
//		final String imgUrl = URLHelper.getFullURL(img, request);
//		IncludeOpenGraphMetaInfoWorker.addOpenGraphMetaInfo("image", imgUrl, rs, null);
//		rs.addLink("image_src", imgUrl);
//	}
//
//	public static void setUrl(final String url, final ArachneRenderSupport rs, HttpServletRequest request, ComponentResources resources) {
//		final String fullUrl = URLHelper.getFullURL(url, request);
//		final String expandedURL = IncludeOpenGraphMetaInfoWorker.addOpenGraphMetaInfo("image", fullUrl, rs, resources);
//		rs.addMetaTag("share_url", expandedURL);
//	}
//
//	public static void setDescription(final String description, final ArachneRenderSupport rs, HttpServletRequest request, ComponentResources resources) {
//		final String desc = IncludeOpenGraphMetaInfoWorker.addOpenGraphMetaInfo("description", description, rs, resources);
//		rs.addMetaTag("description", desc);
//	}
//
//	public static void setTitle(final String title, final ArachneRenderSupport rs, HttpServletRequest request, ComponentResources resources) {
//    	final String ft = IncludeOpenGraphMetaInfoWorker.addOpenGraphMetaInfo("title", title, rs, resources);
//		rs.addMetaTag("title", ft); 
//	}

	public void transform(PlasticClass arg0, TransformationSupport arg1,
			MutableComponentModel arg2) {
		// TODO Auto-generated method stub
		
	}

}
